package app.common.utils.cjt.vo;

import app.common.utils.cjt.ao.PurchaseArrivalCreateAo;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 采购入库单响应
 * @author feizns
 * @since 2023/4/7
 */
@NoArgsConstructor
@Data
public class WmsStockinPurchaseQueryWithDetailVo {
    /**
     * total_count
     */
    private Integer total_count;
    /**
     * order
     */
    private List<Order> order;

    /**
     * Order
     */
    @NoArgsConstructor
    @Data
    public static class Order {
        /**
         * order_no
         */
        private String order_no;
        /**
         * logistics_name
         */
        private String logistics_name;
        /**
         * details_list
         */
        private List<DetailsList> details_list;
        /**
         * tax_amount
         */
        private Double tax_amount;
        /**
         * seq_no
         */
        private Integer seq_no;
        /**
         * created_time
         */
        private Long created_time;
        /**
         * purchase_no
         */
        private String purchase_no;
        /**
         * stockin_id
         */
        private Integer stockin_id;
        /**
         * total_price
         */
        private Double total_price;
        /**
         * logistics_no
         */
        private String logistics_no;
        /**
         * goods_amount
         */
        private String goods_amount;
        /**
         * purchase_id
         */
        private Integer purchase_id;
        /**
         * remark
         */
        private String remark;
        /**
         * goods_count
         */
        private Integer goods_count;
        /**
         * flag_name
         */
        private String flag_name;
        /**
         * warehouse_no
         */
        private String warehouse_no;
        /**
         * total_stockin_price
         */
        private String total_stockin_price;
        /**
         * modified
         */
        private Long modified;
        /**
         * logistics_type_name
         */
        private String logistics_type_name;
        /**
         * provider_name
         */
        private String provider_name;
        /**
         * status
         */
        private Integer status;
        /**
         * check_time
         */
        private Long check_time;
        /**
         * provider_no
         */
        private String provider_no;

        public PurchaseArrivalCreateAo.Dto toPurchaseArrivalCreateAo() {
            PurchaseArrivalCreateAo.Dto dto = new PurchaseArrivalCreateAo.Dto();
            dto.setCode(this.getOrder_no());
            dto.setVoucherDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date(this.getCreated_time())));
            dto.setBusinessType(new PurchaseArrivalCreateAo.Dto.BusinessType("01"));
            dto.setPartner(new PurchaseArrivalCreateAo.Dto.Partner(this.getProvider_no()));
            dto.setMemo(String.format("编号：[%s] 备注：[%s] 类型：[%s] 采购单编号：[%s]", this.getOrder_no(), this.getRemark(), "采购入库单", this.getPurchase_no()));
            dto.setWarehouse(new PurchaseArrivalCreateAo.Dto.Warehouse(this.getWarehouse_no()));
            dto.setExternalCode(this.getOrder_no());
            dto.setVoucherDetails(
                    this.details_list.stream()
                    .map(DetailsList::toVoucherDetails)
                    .collect(Collectors.toList())
            );
            return dto;
        }

        /**
         * DetailsList
         */
        @NoArgsConstructor
        @Data
        public static class DetailsList {
            /**
             * rec_id
             */
            private Integer rec_id;
            /**
             * num
             */
            private Double num;
            /**
             * discount
             */
            private Double discount;
            /**
             * cost_price
             */
            private Integer cost_price;
            /**
             * src_price
             */
            private Double src_price;
            /**
             * tax_price
             */
            private Double tax_price;
            /**
             * tax_amount
             */
            private Double tax_amount;
            /**
             * tax
             */
            private Integer tax;
            /**
             * total_cost
             */
            private Double total_cost;
            /**
             * remark
             */
            private String remark;
            /**
             * goods_name
             */
            private String goods_name;
            /**
             * goods_no
             */
            private String goods_no;
            /**
             * spec_no
             */
            private String spec_no;
            /**
             * spec_name
             */
            private String spec_name;
            /**
             * position_no
             */
            private String position_no;
            /**
             * unit_ratio
             */
            private Integer unit_ratio;
            /**
             * batch_no
             */
            private String batch_no;
            /**
             * stockin_price
             */
            private Double stockin_price;
            /**
             * expire_date
             */
            private String expire_date;
            /**
             * defect
             */
            private Boolean defect;
            /**
             * production_date
             */
            private String production_date;
            /**
             * prop1
             */
            private String prop1;
            /**
             * prop2
             */
            private String prop2;
            /**
             * prop3
             */
            private String prop3;
            /**
             * prop4
             */
            private String prop4;
            /**
             * brand_name
             */
            private String brand_name;
            /**
             * purchase_unit_name
             */
            private String purchase_unit_name;
            /**
             * unit_name
             */
            private String unit_name;

            public PurchaseArrivalCreateAo.Dto.VoucherDetails toVoucherDetails() {
                PurchaseArrivalCreateAo.Dto.VoucherDetails dto = new PurchaseArrivalCreateAo.Dto.VoucherDetails();
                dto.setInventory(new PurchaseArrivalCreateAo.Dto.VoucherDetails.Inventory(this.getSpec_no()));
                dto.setQuantity(this.getNum());
                dto.setOrigDiscountAmount(BigDecimal.valueOf(this.getTotal_cost()));
                dto.setOrigTaxAmount(BigDecimal.valueOf(this.getTax_amount()));
                return dto;
            }
        }
    }
}
